package com.yyl.rbacs.sys.controller;

import com.yyl.rbacs.sys.entity.SysMenu;
import com.yyl.rbacs.sys.entity.SysUser;
import com.yyl.rbacs.sys.service.ISysMenuService;
import com.yyl.rbacs.utils.Constants;
import com.yyl.rbacs.utils.R;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Set;

/**
 * <p>
 * 菜单管理 前端控制器
 * </p>
 *
 * @author YuanSir
 * @since 2022-10-28
 */


@RestController
@RestControllerAdvice
@RequestMapping("/sys/menu")
public class SysMenuController extends AbstractController{
    
    @Autowired
    private ISysMenuService sysMenuService;
    
    @Autowired
    private ISysMenuService menuService;
    
    @Autowired
    private RedisTemplate redisTemplate;
    
    @GetMapping("/nav")
    public R nav(){
    
        SysUser user = getCurrentUser();
    
        List<SysMenu> sysMenuListVos = sysMenuService.getMenuList(user.getUserId());
    
        return R.ok().put("menuList", sysMenuListVos);
    
    }
    
    
    @GetMapping("/list")
    @RequiresPermissions("sys:menu:list")
    public List<SysMenu> list(){
        
        List<SysMenu> sysMenus = menuService.list();
        
        //为菜单数据添加parentName属性值
        HashMap<Long, String> map = new HashMap<>();
    
        sysMenus.forEach(menu -> map.put(menu.getMenuId(), menu.getName()));
    
        sysMenus.forEach(menu -> menu.setParentName(map.get(menu.getParentId())));
    
        //模拟删除菜单相关缓存
        Set keys = redisTemplate.keys("*sys:menu*");
        redisTemplate.delete(keys);
    
        return sysMenus;
    
    }
    
    
    

}
